package com.fragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import model.LTModel;
import model.LTServiceModel;
import model.PeopleModel;
import model.SearchModel;

import org.json.JSONObject;
import org.xutils.x;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import com.dialog.LTDialog;
import com.friehome.app.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.net.HttpRequestUtil;
import com.net.HttpResponse;
import com.net.TTHttpRequestControll;

import adapter.MainAdapter;
import adapter.SearchAdapter;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment3 extends BaseFragment{

	@ViewInject(R.id.text1)
	TextView text1;
	@ViewInject(R.id.text2)
	TextView text2;
	
	@ViewInject(R.id.listview)
	ListView listview;
	
	
	String id;
	String ltID = "";
	String date = "";
	
	List<SearchModel> data = new ArrayList<SearchModel>();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment3, null);
		x.view().inject(this, view);
		
		//id = getArguments().getString("id");
		

		
		
		
		
		
		return view;
	}
	
	@Event(value={R.id.content_btn11,R.id.text1,R.id.text2,R.id.btn_search})
	private void onClick2(View v){
		
		switch (v.getId()) {
		
		case R.id.text1:
			showDateDialog();
			break;
		case R.id.text2:
			LTDialog dialog = new LTDialog(getActivity(), null, new LTDialog.DialogClickListener() {
				
				@Override
				public void onBack(String id, String name) {
					// TODO Auto-generated method stub
					text2.setText(name);
					ltID = id;
				}
			});
			dialog.showDialog();
			break;
		case R.id.btn_search:
//			if (TextUtils.isEmpty(ltID)) {
//				toast("请选择礼厅");
//				return;
//			}
			getData();
			break;

		default:
			break;
		}
	}
	/**
	 * 显示时间窗口
	 */
	public void showDateDialog(){
		try {
	
			Calendar cal = Calendar.getInstance();
			DatePickerDialog dpDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					
					 String month = monthOfYear + 1 >=10 ? ("" + ( monthOfYear + 1) ):( "0" + ( monthOfYear + 1)); 
					 String day = dayOfMonth  >=10 ? ("" + ( dayOfMonth ) ):( "0" + ( dayOfMonth)); 

					 date = year + "-" + month +"-" + day;
					 text1.setText(date); 
					
				}
			},  
                    cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),  
                    cal.get(Calendar.DAY_OF_MONTH));  
            
            dpDialog.show();  
           
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getData(){
		TTHttpRequestControll.httpSearch(getActivity(),ltID,date, new HttpRequestUtil.HttpRonspontCallBack() {
			
			@Override
			public void callBack(HttpResponse result) {
				try {
					if (result.success) {
					//	JSONObject ob=  new JSONObject(result.content);
						data = new Gson().fromJson(result.content, new  TypeToken<List<SearchModel>>(){}.getType() );
						listview.setAdapter(new SearchAdapter(getActivity(), data));
					}else{
						toast(result.errorMsg);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
					
				
			}
		});
	}
	
}
